﻿using HirelSeriesH.Entity;
using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HirelSeriesH
{
    public partial class PowerGateControlForm : UIForm
    {
        List<UIDoubleUpDown> VgPower01 = new List<UIDoubleUpDown>();
        //List<UIDoubleUpDown> VgPower02 = new List<UIDoubleUpDown>();

        List<List<UIDoubleUpDown>> VgPowerList = new List<List<UIDoubleUpDown>>();

        List<UIButton> VgPowerBtnList = new List<UIButton>();

        List<Label> VgPowerStateList = new List<Label>();

        public PowerGateControlForm()
        {
            InitializeComponent();

            VgPower01.Add(VgVoltage01);
            VgPower01.Add(VgUpStep01);
            VgPower01.Add(VgUpInterval01);
            VgPower01.Add(VgDownStep01);
            VgPower01.Add(VgDownInterval01);

            /*VgPower02.Add(VgVoltage02);
            VgPower02.Add(VgUpStep02);
            VgPower02.Add(VgUpInterval02);
            VgPower02.Add(VgDownStep02);
            VgPower02.Add(VgDownInterval02);*/

            VgPowerList.Add(VgPower01);
            //VgPowerList.Add(VgPower02);

            VgPowerBtnList.Add(BtnVg01);
            //VgPowerBtnList.Add(BtnVg02);

            int i = 0;
            foreach (var btn in VgPowerBtnList)
            {
                btn.Click += new EventHandler(this.BtnVg_Click);
                Global.Global.PowerVgList[i].ButtonAction = btn;
                i++;
            }

            VgPowerStateList.Add(VgState01);
            //VgPowerStateList.Add(VgState02);
        }

        private void PowerGateControlForm_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Global.Global.PowerVgList.Count; i++)
            {
                if (Global.Global.PowerVgList[i].TimerDown.Enabled == true)
                {
                    VgPowerBtnList[i].Enabled = false;
                }
                VgPowerList[i][0].Maximum = (double)Global.Global.PowerVgList[i].MaxVoltage;
                VgPowerList[i][1].Maximum = (double)Global.Global.PowerVgList[i].MaxVoltage;
                VgPowerList[i][3].Maximum = (double)Global.Global.PowerVgList[i].MaxVoltage;

                if (Global.Global.PowerVgList[i].IsOpen)
                {
                    VgPowerList[i][0].Enabled = false;
                    VgPowerList[i][1].Enabled = false;
                    VgPowerList[i][2].Enabled = false;
                    VgPowerList[i][3].Enabled = false;
                    VgPowerList[i][4].Enabled = false;
                    VgPowerStateList[i].Text = "已开启";
                    VgPowerBtnList[i].Text = "关闭";
                }
                else
                {
                    VgPowerList[i][0].Enabled = true;
                    VgPowerList[i][1].Enabled = true;
                    VgPowerList[i][2].Enabled = true;
                    VgPowerList[i][3].Enabled = true;
                    VgPowerList[i][4].Enabled = true;
                    VgPowerStateList[i].Text = "未开启";
                    VgPowerBtnList[i].Text = "开启";
                }
                //电压
                VgPowerList[i][0].Value = (double)Global.Global.PowerVgList[i].Voltage;
                //升压步长
                VgPowerList[i][1].Value = (double)Global.Global.PowerVgList[i].UpStep;
                //升压间隔
                VgPowerList[i][2].Value = (double)Global.Global.PowerVgList[i].UpInterval;
                //降压步长
                VgPowerList[i][3].Value = (double)Global.Global.PowerVgList[i].DownStep;
                //降压间隔
                VgPowerList[i][4].Value = (double)Global.Global.PowerVgList[i].DownInterval;
            }
            //监控周期
            this.NudCheckInterval.Value = (double)Global.Global.PowerVgList[0].CheckInterval;
        }

        private void BtnVg_Click(object sender, EventArgs e)
        {
            int index = int.Parse(((UIButton)sender).Tag.ToString()) - 1;
            VGPower VgPower = Global.Global.PowerVgList[index];
            if (VgPower.Port == null || !VgPower.Port.IsOpen)
            {
                UIMessageDialog.ShowMessageDialog(this, "电源通信失败，请检查通信端口和通信线路！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            //电压值
            decimal Voltage = (decimal)VgPowerList[index][0].Value;
            //升压步长
            decimal UpStep = (decimal)VgPowerList[index][1].Value;
            decimal UpStepIn = (decimal)VgPowerList[index][1].Value;
            //升压间隔
            decimal UpInterval = (decimal)VgPowerList[index][2].Value;
            //降压步长
            decimal DownStep = (decimal)VgPowerList[index][3].Value;
            //降压间隔
            decimal DownInterval = (decimal)VgPowerList[index][4].Value;
            //监控周期
            decimal CheckInterval = (decimal)this.NudCheckInterval.Value;

            VgPower.ButtonAction = (UIButton)sender;

            if (!VgPower.IsOpen)
            {
                if (!UIMessageDialog.ShowMessageDialog(this, "是否确定开启" + VgPower.PowerName + "？", UILocalize.AskTitle, true, UIStyle.Colorful))
                {
                    return;
                }
                // 首先检查用户输入的参数是否合法
                /*if (Voltage == 0)
                {
                    UIMessageDialog.ShowMessageDialog(this, "电压值不能为0", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }*/
                if (UpStep > 0 && UpInterval == 0)
                {
                    UIMessageDialog.ShowMessageDialog(this, "请输入升压步进间隔", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                if (DownStep > 0 && DownInterval == 0)
                {
                    UIMessageDialog.ShowMessageDialog(this, "请输入降压步进间隔", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                if (CheckInterval == 0)
                {
                    UIMessageDialog.ShowMessageDialog(this, "监控周期不能为0", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                LoadingForm.LoadingHelper.ShowLoadingScreen();

                if (UpStep >= Voltage || UpStep == 0)
                {
                    UpStep = Voltage;
                }

                //更新全局变量
                VgPower.Voltage = Voltage;
                VgPower.UpStep = UpStepIn;
                VgPower.UpInterval = UpInterval;
                VgPower.DownStep = DownStep;
                VgPower.DownInterval = DownInterval;
                VgPower.CheckInterval = CheckInterval;
                VgPower.IsOpen = true;
                VgPower.IsUpDown = true;
                VgPower.VoltageCurrent = 0;
                VgPower.IsCommunityError = false;

                //获取远程控制权
                /*string strCommand = "SYST:REM" + "\r\n";
                VgPower.Port.Write(strCommand);
                Thread.Sleep(200);
                //获取远程控制权
                VgPower.Port.Write(strCommand);
                Thread.Sleep(200);*/

                //设置初始电流
                VgPower.setMaxCurrent();
                Thread.Sleep(100);
                //设置初始电压为0
                VgPower.setVolt(0);
                Thread.Sleep(200);
                //开启电源输出 
                VgPower.open();
                Thread.Sleep(200);

                VgPower.NumList = VgPowerList[index];
                //VgPower.ButtonAction = (UIButton)sender;
                VgPower.Label = VgPowerStateList[index];
                VgPower.VoltageValue = Voltage;
                VgPower.UpStepVal = UpStep;

                //Thread.Sleep(2000);
                Thread thread = new Thread(new ThreadStart(VgPower.upVoltage));
                thread.Start();
                Thread.Sleep(1500);

                LoadingForm.LoadingHelper.CloseForm();

                //Debug.WriteLine("UpStep:" + Global.Global.PowerVgList[index].UpStep.ToString());

                //更新数据库
                MySqlParameter[] paras = new MySqlParameter[] {
                    new MySqlParameter("@Voltage", VgPower.Voltage.ToString()),
                    new MySqlParameter("@UpStep", UpStepIn.ToString()),
                    new MySqlParameter("@UpInterval", VgPower.UpInterval.ToString()),
                    new MySqlParameter("@DownStep", VgPower.DownStep.ToString()),
                    new MySqlParameter("@DownInterval", VgPower.DownInterval.ToString()),
                    new MySqlParameter("@CheckInterval", VgPower.CheckInterval.ToString()),
                    new MySqlParameter("@ID", VgPower.ID) };
                DBHelper.ExecuteNonQuery(DBSql.UPDATE_HTRB_POWERT_ALL, 1, paras);
            }
            else
            {
                if (!UIMessageDialog.ShowMessageDialog(this, "是否确定关闭" + VgPower.PowerName + "？", UILocalize.AskTitle, true, UIStyle.Colorful))
                {
                    return;
                }

                foreach (var channel in Global.Global.ChannelList)
                {
                    if (channel.State == "running" && channel.VGPower.PowerName == VgPower.PowerName)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "请先关闭此栅极电源对应的测试,再关闭Vg电源。", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }

                if (DownStep >= Voltage || DownStep == 0)
                {
                    DownStep = Voltage;
                }

                //设置步进降压步长
                VgPower.setVolt(VgPower.VoltageCurrent - DownStep);
                VgPower.VoltageCurrent = VgPower.VoltageCurrent - DownStep;
                Debug.WriteLine((index + 1).ToString() + "号Vg电源降压至：" + VgPower.VoltageCurrent);

                VgPower.TimerUp.Enabled = false;
                VgPower.IsUpDown = false;

                LoadingForm.LoadingHelper.ShowLoadingScreen();

                if (DownStep < Voltage)
                {
                    VgPower.IsUpDown = true;
                    double interval = (double)(DownInterval * 1000);
                    VgPower.TimerDown.Interval = interval;
                    VgPower.TimerDown.Start();
                    UIButton btn = (UIButton)sender;
                    btn.Enabled = false;
                }
                if (DownStep == Voltage)
                {
                    //休眠100毫秒
                    Thread.Sleep(150);
                    //关闭电源输出
                    VgPower.close();
                    //更新全局变量
                    VgPower.IsOpen = false;
                    VgPower.RealVoltage = 0;
                    VgPower.RealCurrent = 0;
                }
                Thread.Sleep(1500);
                LoadingForm.LoadingHelper.CloseForm();

                //更新控件状态
                VgPowerList[index][0].Enabled = true;
                VgPowerList[index][1].Enabled = true;
                VgPowerList[index][2].Enabled = true;
                VgPowerList[index][3].Enabled = true;
                VgPowerList[index][4].Enabled = true;
                VgPowerStateList[index].Text = "未开启";
                UIButton button = (UIButton)sender;
                button.Text = "开启";
            }

        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Global.Global.PowerVgList.Count; i++)
            {
                //更新全局变量
                Global.Global.PowerVgList[i].Voltage = (decimal)VgPowerList[i][0].Value;
                Global.Global.PowerVgList[i].UpStep = (decimal)VgPowerList[i][1].Value;
                Global.Global.PowerVgList[i].UpInterval = (decimal)VgPowerList[i][2].Value;
                Global.Global.PowerVgList[i].DownStep = (decimal)VgPowerList[i][3].Value;
                Global.Global.PowerVgList[i].DownInterval = (decimal)VgPowerList[i][4].Value;
                Global.Global.PowerVgList[i].CheckInterval = (decimal)NudCheckInterval.Value;
                //更新数据库
                MySqlParameter[] paras = new MySqlParameter[] {
                    new MySqlParameter("@UpStep", Global.Global.PowerVgList[i].UpStep.ToString()),
                    new MySqlParameter("@UpInterval", Global.Global.PowerVgList[i].UpInterval.ToString()),
                    new MySqlParameter("@DownStep", Global.Global.PowerVgList[i].DownStep.ToString()),
                    new MySqlParameter("@DownInterval", Global.Global.PowerVgList[i].DownInterval.ToString()),
                    new MySqlParameter("@CheckInterval", Global.Global.PowerVgList[i].CheckInterval.ToString()),
                    new MySqlParameter("@ID", Global.Global.PowerVgList[i].ID) };
                DBHelper.ExecuteNonQuery(DBSql.UPDATE_HTRB_POWERT_UPDOWN, 1, paras);
            }
            UIMessageDialog.ShowMessageDialog(this, "电源配置保存成功", UILocalize.SuccessTitle, false, UIStyle.Colorful);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
